home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / netvis / NetvisSession.h < prev    next >
C/C++ Source or Header  |  2001-04-25  |  4KB  |  136 lines

  1. #ifndef NETVISSESSION_H__
  2. #define    NETVISSESSION_H__
  3.  
  4. #if _MSC_VER >= 1000
  5. #pragma once
  6. #endif // _MSC_VER >= 1000
  7.  
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11.  
  12. #include "cc++/socket.h"
  13. #include "cc++/thread.h"
  14. #include "ZHLTThread.h"
  15.  
  16. #include <string>
  17. #include <deque>
  18. #include <list>
  19. #include <map>
  20. #include <set>
  21.  
  22. #include "ReferenceArray.h"
  23. #include "ReferencePtr.h"
  24.  
  25. #include "basictypes.h"
  26.  
  27. class basePacket;
  28. class VIS_PACKET;
  29. class NetvisSession;
  30. class NetvisSocketServer;
  31.  
  32. class Packet
  33. {
  34. public:
  35.     Packet(const basePacket* packet);
  36.     Packet(const Packet& other)
  37.     { copy(other); }
  38.     Packet& operator=(const Packet& other)
  39.     { copy(other); return *this; }
  40.     const VIS_PACKET& data() const;
  41.  
  42. protected:
  43.     void copy(const Packet& other)
  44.     {
  45.         m_Payload = other.m_Payload;
  46.     }
  47.  
  48. protected:
  49.     ReferenceArray<CHAR>    m_Payload;
  50. };
  51.  
  52.  
  53. class NetvisSocket : public TCPSocket
  54. {
  55. protected:
  56.     virtual bool OnAccept(const InetHostAddress &ia, tpport_t port);
  57. public:
  58.     NetvisSocket(const InetAddress &ia, tpport_t port);
  59. };
  60.  
  61.  
  62. class NetvisSession : public ZHLTThread
  63. {
  64. private:
  65.     virtual void Run();
  66.     virtual void Final() {};
  67. public:
  68.     NetvisSession(NetvisSocketServer* parent, TCPSocket& server);           // Netvis Server Session
  69.     NetvisSession(const InetHostAddress &host, tpport_t port);              // Netvis Client Session
  70.     virtual ~NetvisSession()
  71.     {
  72.         Terminate();
  73.     }
  74.  
  75.     void SendPacket(const basePacket* packet);
  76.     void DisplayClientStats();
  77.     void DisplayServerStats();
  78.  
  79.  
  80.  
  81. protected:
  82.     bool WritePacket(const VIS_PACKET& packet);       // returns true on success (false == assume socket died on us)
  83.     bool ReadPacket(VIS_PACKET& packet);              // returns true on success (false == assume socket died on us)
  84.  
  85. protected:
  86.     TCPStream                           m_TCPStream;
  87.     NetvisSocketServer*                 m_Parent;           // server variable for handling socket disconnects
  88.  
  89. public:
  90.     long                                m_ClientId;         // server var
  91.     bool                                m_Active;           // server var (both technically)
  92.     std::string                         m_RemoteHostname;   // both
  93.  
  94.     ReferenceCounter                    m_SyncIndex;        // server var
  95.     ReferenceCounter                    m_PortalCount;      // server var
  96.     ReferenceCounter                    m_PacketsCount;     // number of pending packets (prevents unnecessary calls to Mutex locking)
  97.     std::deque< Packet >                m_Packets;          // both (outbound packets only)
  98.     Mutex                               m_PacketsLock;      // both (outbound packets only)
  99.     std::set< long >                    m_WorkDone;         // server var
  100.     Mutex                               m_WorkDoneLock;     // server var
  101.     std::deque< long >                  m_WorkQueue;        // server var
  102.     Mutex                               m_WorkQueueLock;    // server var
  103.  
  104.     typedef std::deque< long >::iterator      WorkQueue_i;
  105.     typedef std::set< long >::iterator        WorkDone_i;
  106.     typedef std::deque< Packet >::iterator    Packet_i;
  107. };
  108.  
  109.  
  110.  
  111. class NetvisSocketServer : public ZHLTThread
  112. {
  113. private:
  114.     virtual void Run();
  115.  
  116. public:
  117.     NetvisSocketServer(tpport_t port);
  118.     virtual ~NetvisSocketServer()
  119.     {
  120.         Terminate();
  121.     }
  122.     static void DisplayClientStats(long clientid, unsigned long portalcount, const char* hostname, bool inactive);
  123.     void DisplayServerStats();
  124.  
  125. protected:
  126.     tpport_t                        m_Port;
  127.     std::deque< NetvisSession* >    m_Clients;
  128.     Mutex                           m_ClientsLock;
  129.  
  130.     typedef std::deque< NetvisSession* >::iterator Clients_i;
  131. };
  132.  
  133. extern NetvisSession*  g_ClientSession;
  134.  
  135. #endif//NETVISSESSION_H__
  136.